package ru.nstu.isma.core.hsm.pde;

import ru.nstu.isma.core.hsm.HMDerivativeEquation;

import java.util.LinkedList;
import java.util.List;

/**
 * by
 * Bessonov Alex.
 * Date: 04.12.13 Time: 0:25
 */
public class HMPartialDerEquation extends HMDerivativeEquation {

    private List<HMPDEBound> boundaries = new LinkedList<HMPDEBound>();

    private List<HMApproximateVariable> variables = new LinkedList<HMApproximateVariable>();

    public HMPartialDerEquation(String code) {
        super(code);
    }

    public HMPartialDerEquation(HMDerivativeEquation eq) {
        super(eq.getCode());
        setInitial(eq.getInitial());
        setRightPart(eq.getRightPart());
    }

    public void addBound(HMPDEBound bb) {
        boundaries.add(bb);
    }

    public void addApxVar(HMApproximateVariable vv) {
        variables.add(vv);
    }

    public List<HMPDEBound> getBoundaries() {
        return boundaries;
    }

    public List<HMApproximateVariable> getVariables() {
        return variables;
    }

    public boolean isContains(HMApproximateVariable v) {
        for (HMApproximateVariable vvv : variables) {
            if (vvv.getCode().equals(v.getCode())) {
                return true;
            }
        }
        return false;
    }

    public HMPDEBound getBound(HMPDEBound.Type type, HMApproximateVariable apx) {
        List<HMPDEBound> left = new LinkedList<HMPDEBound>();
        for (HMPDEBound b : boundaries) {
            if (b.getSide() == type || b.getSide() == HMPDEBound.Type.BOTH) {
                if (b.getAtApxVar().getCode().equals(apx.getCode())) {
                    left.add(b);
                }
            }
        }
        if (left.size() != 1) {
            throw new RuntimeException("Incorrect count of boundaries for " + type.name() + " side(s) of " + getCode() + ": " + left.size());
        }
        return left.get(0);
    }
}
